/**
 *
 */
package com.yangtao.prototype.system.organization.service.impl;

import com.yangtao.framework.service.BaseService;
import com.yangtao.framework.util.StringHelper;
import com.yangtao.prototype.system.dao.OrganizationDao;
import com.yangtao.prototype.system.domain.Organization;
import com.yangtao.prototype.system.organization.service.OrganizationService;
import com.yangtao.prototype.system.organization.web.OrganizationModel;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 杨涛
 */
@Service
public class OrganizationServiceImpl extends BaseService implements OrganizationService {
	@Autowired
	private OrganizationDao organizeDao;

	/**
	 * 编号和名称不可重复
	 *
	 * @param model
	 * @return
	 */
	@Override
	public OrganizationModel addOrganization(OrganizationModel model) {
		//VO对象
		Organization organizationVo = model.getOrganization();
		//PO对象
		Organization organizationPo = new Organization();
		//从VO copy值到PO
		mapper.map(organizationVo, organizationPo);
		//默认情况下是有效机构
		organizationPo.setActive(true);
		//设置组织机构的parent
		if (StringUtils.isNotEmpty(model.getParentId())) {
			Organization parent = organizeDao.load(model.getParentId());
			organizationPo.setSupOrganization(parent);
			organizationPo.setLevel(parent.getLevel() + 1);
		}
		organizeDao.save(organizationPo);
		//设置组织机构的path
		if (organizationPo.getSupOrganization() != null) {
			organizationPo.setPath(organizationPo.getSupOrganization().getPath() + "/" + organizationPo.getId());
		} else {
			organizationPo.setPath(organizationPo.getId());
		}
		organizationVo.setId(organizationPo.getId());
		organizationVo.setPath(organizationPo.getPath());
		model.addInfo("save.success");
		return model;
	}

	/**
	 * 根据对象相关数据信息查询符合数据条件分页数据集
	 *
	 * @param model
	 * @return
	 */
	@Override
	public OrganizationModel list(OrganizationModel model) {
		organizeDao.findPageByExample(model.getPage(), model.getOrganization());
		for (Organization o : (List<Organization>) model.getPage().getItems()) {
			o.getSupOrganization();
			o.setSubOrganization(null);
		}
		return model;
	}

	/**
	 * 查询组织机构树
	 *
	 * @param model
	 * @return
	 */
	@Override
	public OrganizationModel findOrganizationTree(OrganizationModel model) {
		String orgId = model.getUser().getOrganizationId();
		model.setOrganizationStore(organizeDao.getOrganizationStroe(orgId));
		return model;
	}

	/**
	 * 根据组织机构ID加载组织机构单条数据信息
	 *
	 * @param model
	 * @return
	 */
	@Override
	public OrganizationModel loadOrganization(OrganizationModel model) {
		if (model.getOrganization() != null
				&& StringHelper.isNotEmpty(model.getOrganization().getId())) {
			model.setOrganization(organizeDao.load(model.getOrganization().getId()));
		}
		return model;
	}

	/**
	 * 根据组织机构对象进行删除操作, 以下注意项为Y不可删除
	 * 注意项：
	 * 1.判断是否有子组织机构
	 * 2.判断组织机构是否有用户
	 * 3.机构下是否有角色
	 * 4.机构下是否有仓库
	 *
	 * @param model
	 * @return
	 */
	@Override
	public OrganizationModel deleteOrganization(OrganizationModel model) {
		Organization organization = organizeDao.load(model.getOrganization().getId());
		// 错误标记
		boolean err = false;
		// 如果有子机构
		if (organization.hasChildren()) {
			// 判断是否忽略子机构
			if (model.getIngoreSubOrg() == 0) {
				model.addWarning("has.subOrg");
				return model;
			}
		}
		// 判断是否有用户
		if (organization.hasUser()) {
			model.addError("has.users");
			err = true;
		}
		// 判断是否有角色
		if (organization.hasRole()) {
			model.addError("has.roles");
			err = true;
		}
		// 如果有错误
		if (err)
			return model;
		// 删除对象
		organizeDao.delete(organization);
		model.addInfo("delete.success");
		return model;
	}

	/**
	 * 根据对象相关信息数据对组织机构进行新增或修改
	 * 编号不可修改，名称可修改不可重复
	 *
	 * @param model
	 * @return
	 */
	@Override
	public OrganizationModel updateOrganization(OrganizationModel model) {
		//VO
		Organization organizationVo = model.getOrganization();
		//主键
		String organizationId = organizationVo.getId();
		//PO
		Organization organizationPo = organizeDao.load(organizationId);
		//复制属性
		mapper.map(organizationVo, organizationPo);
		model.addInfo("update.success");
		return model;
	}

}
